﻿using System;
using System.Linq;
using Ones.ServiceHumanResourceManagement;

namespace Ones.SysAdmin
{
    public partial class HrMgmtJobPage : System.Web.UI.Page
    {
        private ServiceHumanResourceManagementClient c = null;
        private Job job = null;
        private string itemId = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["cmd"] != null)
                {
                    loadData();

                    if (Session["cmd"].ToString().Equals("delete"))
                    {
                        txtTitle.Enabled = false;
                        txtDescr.Enabled = false;
                    }
                }
            }
        }

        public void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                c = new ServiceHumanResourceManagementClient("hrWsHttpBinding");
                job = new Job();

                job.jobTitle = txtTitle.Text;
                job.jobDescription = txtDescr.Text;

                if (c.addJob(job))
                {
                    c.Close();

                    Session.Remove("cmd");

                    Response.Redirect("~/SysAdmin/HrListDivJobPage.aspx");
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                c = new ServiceHumanResourceManagementClient("hrWsHttpBinding");
                job = new Job();

                job.jobId = Session["id"].ToString();
                job.jobTitle = txtTitle.Text;
                job.jobDescription = txtDescr.Text;

                if (c.updateJob(job))
                {
                    c.Close();

                    Session.Remove("cmd");

                    Response.Redirect("~/SysAdmin/HrListDivJobPage.aspx");
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                c = new ServiceHumanResourceManagementClient("hrWsHttpBinding");
                itemId = Session["id"].ToString();

                if (c.deleteJob(itemId))
                {
                    c.Close();

                    Session.Remove("cmd");

                    Response.Redirect("~/SysAdmin/HrListDivJobPage.aspx");
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/SysAdmin/HrListDivJobPage.aspx");
        }

        private void loadData()
        {
            c = new ServiceHumanResourceManagementClient("hrBasicHttpBinding");
            
            itemId = Session["id"].ToString();
            job = c.searchJobById(itemId);

            txtTitle.Text = job.jobTitle;
            txtDescr.Text = job.jobDescription;

            c.Close();
        }
    }
}